import { BrowserWindow, app } from 'electron';
import ipcMainInit from './communication/index';
import path from 'path';
import installExtension from './installExtension';

let window = null;

function createMainWindow(
  options = {},
  url = `http://localhost:${process.env.PORT}/#/`,
) {
  const window = new BrowserWindow({
    width: 1080,
    height: 900,
    transparent: true, //设置窗口为透明
    frame: true, //无边框窗口
    // alwaysOnTop: true,//置顶窗口
    skipTaskbar: false, //任务栏显示窗口
    webPreferences: {
      //开启node支持
      nodeIntegration: true,
      contextIsolation: false,
      //开启webview支持
      webviewTag: true,
      preload: path.resolve(__dirname, '../preload/preload.js'), //预加载脚本
      // webSecurity: true,
      plugins: true,
    },
    ...options,
  });
  window
    .loadURL(
      app.isPackaged //判断是否打包
        ? path.resolve(__dirname, '../browser/index.html')
        : url,
    )
    .then(() => {
      console.log('程序已打开');
      window.webContents.openDevTools();
    });
  return window;
}

app.on('ready', () => {
  window = createMainWindow();
  ipcMainInit(window, app);
});
app.whenReady().then(() => {
  //判断是否打包
  if (!app.isPackaged) {
    installExtension();
  }
});
